package com.ude.pim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ude.pim.common.R;
import com.ude.pim.dto.CaseSubCommentDTO;
import com.ude.pim.entity.CaseSub;
import com.ude.pim.entity.CaseSubComment;
import com.ude.pim.mapper.CaseSubCommentMapper;
import com.ude.pim.service.CaseSubCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ude.pim.service.CaseSubService;
import com.ude.pim.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yb
 * @since 2023-08-08
 */
@Service
@Transactional
public class CaseSubCommentServiceImpl extends ServiceImpl<CaseSubCommentMapper, CaseSubComment> implements CaseSubCommentService {
    @Autowired
    private CaseSubService caseSubService;

    @Autowired
    private UserService userService;
    @Override
    public R<List<CaseSubComment>> getListByCaseSubId(Long id) {
        LambdaQueryWrapper<CaseSubComment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CaseSubComment::getCaseSubId,id);
        lqw.eq(CaseSubComment::getStatus,1);
        return R.success(this.list(lqw));
    }

    /**
     * 1. 先获取到所有未审核的备注
     * 2. 根据caseSubId获取到专案子流程信息
     * 3. 根据子流程信息的专案ID获取专案名称
     * 4. 根据子流程信息的子流程ID获取子流程名称
     * 5. 根据createUserId获取到创建人名称
     * @return
     */
    @Override
    public R<List<CaseSubCommentDTO>> getPendingReview() {
        LambdaQueryWrapper<CaseSubComment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CaseSubComment::getStatus,0);
        //1. 先获取到所有未审核的备注
        List<CaseSubComment> list = this.list(lqw);

        /* * 2. 根据caseSubId获取到专案子流程信息
         * 3. 根据子流程信息的专案ID获取专案名称
         * 4. 根据子流程信息的子流程ID获取子流程名称
         * 5. 根据createUserId获取到创建人名称*/
        List<CaseSubCommentDTO> collect = comment2DTO(list);
        return R.success(collect);
    }

    //根据用户id和状态获取备注列表
    @Override
    public R<List<CaseSubCommentDTO>> getByUserAndStatus(CaseSubComment caseSubComment) {
        LambdaQueryWrapper<CaseSubComment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CaseSubComment::getStatus,caseSubComment.getStatus());
        lqw.eq(CaseSubComment::getCreateUser,caseSubComment.getCreateUser());
        List<CaseSubComment> list = this.list(lqw);
        return R.success(comment2DTO(list));
    }

    //从普通实体对象转换为DTO对象
    public List<CaseSubCommentDTO> comment2DTO(List<CaseSubComment> list){
        return list.stream().map(s -> {
            CaseSubCommentDTO dto = new CaseSubCommentDTO();
            BeanUtils.copyProperties(s, dto);

            //2. 根据caseSubId获取到专案子流程信息
            Long caseSubId = s.getCaseSubId();
            CaseSub caseSub = caseSubService.getById(caseSubId);

            //3. 根据子流程信息的专案ID获取专案名称
            String caseName = caseSubService.getCaseName(caseSub.getCaseId());
            dto.setCaseName(caseName);

            //4. 根据子流程信息的子流程ID获取子流程名称
            String subName = caseSubService.getSubName(caseSub.getSubId());
            dto.setSubName(subName);

            //5. 根据createUserId获取到创建人名称
            String createName = userService.getById(s.getCreateUser()).getName();
            dto.setCreateName(createName);

            return dto;
        }).collect(Collectors.toList());
    }
}
